//------------------------------------------------------------------------------
// <copyright company="Telligent Systems">
//     Copyright (c) Telligent Systems Corporation.  All rights reserved.
// </copyright> 
//------------------------------------------------------------------------------

using System;
using CommunityServer.Components;

namespace CommunityServer.Files.Components
{

	/// <summary>
	/// Summary description for GalleryPermission.
	/// </summary>
	public class FileGalleryPermission : PermissionBase 
	{

		#region Public Properites - Permissions

		public virtual bool View 
		{
			get{ return GetBit( Permission.View ); }
		}

		public virtual bool Download
		{
			get { return GetBit(Permission.Read); }
		}

		public virtual bool Post 
		{
			get{ return GetBit( Permission.Post ); }
		}

		public virtual bool Reply 
		{
			get{ return GetBit( Permission.Reply ); }
		}
        
		public virtual bool Edit 
		{
			get{ return GetBit( Permission.Edit ); }
		}
        
		public virtual bool Delete 
		{
			get{ return GetBit( Permission.Delete ); }
		}
        
		public virtual bool Vote 
		{
			get{ return GetBit( Permission.Vote ); }
		}

		public virtual bool Moderate 
		{
			get{ return GetBit( Permission.Moderate ); }
		}

		public virtual bool Administer 
		{
			get{ return GetBit( Permission.Administer ); }
		}
		
		public virtual bool SystemAdminister 
		{
			get{ return GetBit( Permission.SystemAdmin ); }
		}

		public virtual bool RemoteAttachment
		{
			get { return GetBit( Permission.RemoteAttachment ); }
		}

		public virtual bool LocalAttachment
		{
			get { return GetBit (Permission.LocalAttachment ); }
		}
		#endregion

		public static bool Validate(Section section, Permission permission, User user, Post p)
		{
			if(section == null || section.PermissionSet == null || user == null )
				return false;

			bool bReturn = true;
			FileGalleryPermission fpFinal = section.ResolvePermission(user) as FileGalleryPermission;

			if((permission == (permission | Permission.Administer)) && bReturn )
				bReturn &= fpFinal.Administer;

			if((permission == (permission | Permission.Delete)) && bReturn ) 
				bReturn &= fpFinal.Delete;

			if((permission == (permission | Permission.Edit)) && bReturn )
				bReturn &= fpFinal.Edit;

			if((permission == (permission | Permission.Moderate)) && bReturn )
				bReturn &= fpFinal.Moderate;

			if((permission == (permission | Permission.Post)) && bReturn )
				bReturn &= fpFinal.Post;

			if((permission == (permission | Permission.Reply)) && bReturn )
				bReturn &= fpFinal.Reply;

			if((permission == (permission | Permission.View)) && bReturn )
				bReturn &= fpFinal.View;

			if((permission == (permission | Permission.Read)) && bReturn )
				bReturn &= fpFinal.Download;

			if((permission == (permission | Permission.Vote)) && bReturn )
				bReturn &= fpFinal.Vote;

			if ((permission == (permission | Permission.RemoteAttachment)) && bReturn)
				bReturn &= fpFinal.RemoteAttachment;

			if ((permission == (permission | Permission.LocalAttachment)) && bReturn)
				bReturn &= fpFinal.LocalAttachment;

			return bReturn;
		}

		#region AccessCheck
		public static void AccessCheck(Section section, Permission permission, User user, Post post)
		{
			if (!Validate(section, permission, user, post))
				throw new CSException(CSExceptionType.AccessDenied);
		}
		#endregion

	}

}
